package controller

import (
	"mumu_stock_trad/model"
	"net/http"

	"github.com/gin-gonic/gin"
)

// 模拟所有异步路由数据（也可以放 JSON 文件中读取）
var allRoutes = []model.Route{
	{
		Path:       "/permission",
		Component:  "layout/Layout",
		Redirect:   "/permission/index",
		AlwaysShow: true,
		Meta: &model.Meta{
			Title: "Permission",
			Icon:  "lock",
			Roles: []string{"admin", "editor"},
		},
		Children: []model.Route{
			{
				Path:      "page",
				Component: "views/permission/page",
				Name:      "PagePermission",
				Meta: &model.Meta{
					Title: "Page Permission",
					Roles: []string{"admin"},
				},
			},
			{
				Path:      "directive",
				Component: "views/permission/directive",
				Name:      "DirectivePermission",
				Meta: &model.Meta{
					Title: "Directive Permission",
				},
			},
		},
	},
	{
		Path:      "/icon",
		Component: "layout/Layout",
		Children: []model.Route{
			{
				Path:      "index",
				Component: "views/icons/index",
				Name:      "Icons",
				Meta: &model.Meta{
					Title: "Icons",
					Icon:  "icon",
				},
			},
		},
	},
}

// 接收角色并返回过滤后的路由
func GetUserRoutes(c *gin.Context) {
	var req struct {
		Roles []string `json:"roles"` // 前端传来的角色列表
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数错误"})
		return
	}

	// 根据角色过滤路由
	filteredRoutes := FilterRoutes(allRoutes, req.Roles)

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"data": filteredRoutes,
	})
}
